package cn.kgc.controller;

import cn.kgc.base.dto.LockStockDTO;
import cn.kgc.entity.WmsWareInfo;
import cn.kgc.service.WmsWareInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.kgc.entity.WmsWareSku;
import cn.kgc.service.WmsWareSkuService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 商品库存(WmsWareSku)表控制层
 *
 * @author makejava
 * @since 2022-12-07 10:41:17
 */
@RestController
@RequestMapping("wms/WareSku")
@Api
@Validated
public class WmsWareSkuController {
    /**
     * 服务对象
     */
    @Resource
    private WmsWareSkuService wmsWareSkuService;

    @Resource
    private WmsWareInfoService wmsWareInfoService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @PutMapping("lockStock")
    @CrossOrigin
    @ApiOperation(value = "锁定库存信息",httpMethod = "PUT",
            produces = "application/json;charset=utf-8",response = Result.class)
    public boolean lockStock(@RequestBody List<LockStockDTO> lockStockDTOList) {

        boolean result = wmsWareSkuService.lockStock(lockStockDTOList);
//        if (result){
//            //向消息队列中发送信息，信息是什么？
//            //消息队列作用是释放库存，发的信息能够帮助我们释放库存的信息
//            //怎么释放库存？ wms_ware_sku
//            // sku_id  ware_id  ulock_stock_num  orderSn
//            //方式1：创建实体类，实体类封装数据
//            //方式2：使用wms_ware_order_task表
//            rabbitTemplate.convertAndSend("stock.exchange",
//                    "lock.stock",lockStockDTOList);
//        }
        return result;
    }




    @GetMapping("/hasStock")
    @CrossOrigin
    @ApiOperation(value="判断商品是否还有库存",httpMethod = "GET",
            produces = "application/json;charset=utf-8",response = Result.class)
    @ApiImplicitParam(name = "skuId",value = "商品的id",paramType = "query",dataType = "long")
    public  boolean hasStock(@RequestParam(name = "skuId") Long skuId){
        //查询当前sku的总库存量
        LambdaQueryWrapper<WmsWareSku>lambdaQueryWrapper=new LambdaQueryWrapper<>();
      lambdaQueryWrapper.eq(WmsWareSku::getSkuId,skuId);
        List<WmsWareSku> wmsWareSkuList = wmsWareSkuService.list(lambdaQueryWrapper);
        long stock=0;  //总库存
        long lockStock=0;  //总的锁定库存
        //总库存 与 总的锁定数据
        for (WmsWareSku wmsWareSku : wmsWareSkuList) {
            stock+=wmsWareSku.getStock();
            lockStock+=wmsWareSku.getStockLocked();
        }
        return  stock>lockStock;

    }

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数 默认值5", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "skuName", value = " skuName", paramType = "query", dataType = "string"),
    })

    @CrossOrigin
    public IPage<WmsWareSku> selectAll(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @RequestParam(defaultValue="")String skuName) {
        LambdaQueryWrapper<WmsWareSku>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(skuName),WmsWareSku::getSkuName,skuName);
        IPage<WmsWareSku> page = new Page(current, pageSize);
        wmsWareSkuService.page(page,lambdaQueryWrapper);
        List<WmsWareSku> wareSkuList = page.getRecords();
        for (WmsWareSku wmsWareSku : wareSkuList) {
            Long wareId = wmsWareSku.getWareId();
            WmsWareInfo wareInfo = wmsWareInfoService.getById(wareId);
            if (wareInfo!=null){
                wmsWareSku.setWareName(wareInfo.getName());
            }
        }
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public WmsWareSku selectOne(@PathVariable Serializable id) {
        return this.wmsWareSkuService.getById(id);
    }





    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.wmsWareSkuService.removeById(id);
    }
}
